package com.uphone.kingmall.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.china.zhuoyue.R;
import com.uphone.kingmall.activity.HomeCatListActivity;
import com.uphone.kingmall.bean.CatsListBean;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GridViewForScrollView;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.intent.IntentUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 右侧主界面ListView的适配器
 *
 * @author Administrator
 */
public class HomeAdapter extends BaseAdapter {

    private Context context;
    private List<CatsListBean.CatsBean> foodDatas;

    public HomeAdapter(Context context, List<CatsListBean.CatsBean> foodDatas) {
        this.context = context;
        this.foodDatas = foodDatas;
    }

    @Override
    public int getCount() {
        return foodDatas == null ? 0 : foodDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return foodDatas.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHold viewHold = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.kingmall_item_home, null);
            viewHold = new ViewHold();
            viewHold.gridView = (GridViewForScrollView) convertView.findViewById(R.id.gridView);
            viewHold.blank = (TextView) convertView.findViewById(R.id.blank);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        HomeItemAdapter adapter = new HomeItemAdapter(context, foodDatas.get(position).getChildren());
        viewHold.blank.setText(foodDatas.get(position).getCatName() + "");
        viewHold.gridView.setAdapter(adapter);
        viewHold.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                CatsListBean.CatsBean.ChildrenBean bean = foodDatas.get(position).getChildren().get(position2);
                IntentUtils.getInstance().with(context, HomeCatListActivity.class)
                        .putInt(ConstansUtils.ID, bean.getId())
                        .putString(ConstansUtils.TITLE, bean.getCatName())
                        .putString(ConstansUtils.URL, MyUrl.getCatGoods).start();
            }
        });
        return convertView;
    }

    public void setList(ArrayList<CatsListBean.CatsBean> catsList) {
        this.foodDatas = catsList;
        notifyDataSetChanged();
    }

    private static class ViewHold {
        private GridViewForScrollView gridView;
        private TextView blank;
    }

}
